﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Commons {
    class PropertyCategoryBC : IPropertyCategory, IPropertyCategoryAdmin {

        #region IPropertyCategoryAdmin Members

        public bool Insert(PropertyCategoryEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                if (item.DateCreated == DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }

                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Add City to DB
                result = PropertyCategoryDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyCategoryBC.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool Update(PropertyCategoryEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Add City to DB
                result = PropertyCategoryDA.Update(item, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyCategoryBC.Update({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public bool UpdateOrdering(List<PropertyCategoryEnt> items) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                for (int i = 0; i < items.Count;i++ ) {
                    if (PropertyCategoryDA.UpdateOrdering(items[i], connectionName, dbCon, dbTrans) == 0) {
                        return false;
                    }
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyCategoryBC.UpdateOrdering({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(items)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        public List<PropertyCategoryEnt> GetAll() {
            try {
                return PropertyCategoryDA.GetByStatus((EPropertyCategoryStatus)(255), ConfigInfo.ConnectionName, null, null);
            }
            catch (System.Exception ex) {
                LogUtils.Error("VISS.Commons.PropertyCategoryBC.GetAll(): \n" + ex.ToString());
                return new List<PropertyCategoryEnt>();
            }
        }

        #endregion

        #region IPropertyCategory Members

        public List<PropertyCategoryEnt> GetShowedList() {
            try {
                return PropertyCategoryDA.GetByStatus(EPropertyCategoryStatus.Show, ConfigInfo.ConnectionName, null, null);
            }
            catch (System.Exception ex) {
                LogUtils.Error("VISS.Commons.PropertyCategoryBC.GetShowedList(): \n" + ex.ToString());
                return new List<PropertyCategoryEnt>();
            }
        }

        #endregion
    }
}
